package com.py.chat.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.py.chat.bean.ChatLog;
import com.py.chat.bean.Gloable;
import com.py.chat.bean.User;
import com.py.chat.utils.DBUtils;
import com.py.chat.utils.PropertiesUtil;

public class ChatLogDao {

	//添加聊天记录
	public void addLog(ChatLog chatLog){
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = DBUtils.getConnection();
			ps = conn.prepareStatement("insert into chatlog(id,msg,create_user_id,create_date) value(?,?,?,?)");
			ps.setString(1,UUID.randomUUID().toString());						
			ps.setString(2, chatLog.getMsg());													
			ps.setString(3, chatLog.getUser().getId());				
			Timestamp timestamp = new Timestamp(chatLog.getCreateDate());
			ps.setTimestamp(4, timestamp);  
			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	// 删除聊天记录
	public void removeChatLogById(String id){
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = DBUtils.getConnection();
			ps = conn.prepareStatement("delete from chatlog where id = ?");
			ps.setString(1,id);						
			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	// 获取所有聊天记录
	public List<ChatLog> getAllList(){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<ChatLog> chatLogs = new ArrayList<>();
		try {
			conn = DBUtils.getConnection();
			ps = conn.prepareStatement("select a.id,a.msg,a.create_date,b.id as userId,b.name as userName,b.password as userPassword ,b.level as userLevel,b.exp as userExp ,b.last_send_msg_time as lastSendMsgTime,b.sk_num as skNum from chatlog  a LEFT JOIN user b ON a.create_user_id = b.id; ");
			rs = ps.executeQuery();
			while(rs.next()){
				ChatLog chatLog = new ChatLog();
				chatLog.setId(rs.getString("id"));
				chatLog.setMsg(rs.getString("msg"));
				chatLog.setCreateDate(rs.getTimestamp("create_date").getTime());
				//TODO 时间
				String id = rs.getString("userId");
				User userMap = Gloable.userMap.get(id);
				if(userMap != null	){
					chatLog.setUser(userMap);
				}else{
					
				}
				User user = new User();
				user.setId(rs.getString("userId"));
				user.setName(rs.getString("userName"));
				user.setPassword(rs.getString("userPassword"));
				user.setLevel(rs.getInt("userLevel"));
				user.setExp(rs.getInt("userExp"));
				if(rs.getTimestamp("lastSendMsgTime") != null){
					user.setLastSendMsgTime(rs.getTimestamp("lastSendMsgTime").getTime());
				}
				user.setSkNum(rs.getInt("skNum"));
				chatLog.setUser(user);
				chatLogs.add(chatLog);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return chatLogs;
	}
	
	
}
